﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DGRobot
{
    public partial class SelectServiceFrm : Form
    {
        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                ExitApp();
            }
        }
        private void ExitApp()
        {
            var msgRet = MessageBox.Show("您确定要退出吗?", "盾构小助手", MessageBoxButtons.YesNo);
            if (msgRet == DialogResult.Yes)
            {
                this.Close();
                System.Environment.Exit(0);
            }
        }
        protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg, System.Windows.Forms.Keys keyData)
        {
            int WM_KEYDOWN = 256;
            int WM_SYSKEYDOWN = 260;
            if (msg.Msg == WM_KEYDOWN | msg.Msg == WM_SYSKEYDOWN)
            {
                switch (keyData)
                {
                    case Keys.Escape:
                        ExitApp();
                        break;
                }
            }
            return false;
        }
        public SelectServiceFrm()
        {
            InitializeComponent();
        }

        public void StartRobot()
        {
            RobotDriver.Instance.OnSpeakStart = RobotDriver_OnSpeakStart;
            RobotDriver.Instance.OnListenStart = RobotDriver_OnListenStart;
            RobotDriver.Instance.OnSpeakCompleted = RobotDriver_OnSpeakCompleted;
            RobotDriver.Instance.OnListening = RobotDriver_OnListening;
            RobotDriver.Instance.OnListenCompleted = RobotDriver_OnListenCompleted;

            RobotDriver.Instance.StartSpeak(nameof(SelectServiceFrm), "您需要选择什么服务?");
            lastHasWord = DateTime.Now;
            this.timer1.Start();
        }

        DateTime lastHasWord=DateTime.Now;
        public void StopRobot()
        {
            RobotDriver.Instance.StopSpeak(false);
            RobotDriver.Instance.StopListen();
            this.timer1.Stop();
            Thread.Sleep(200);
        }

        private void RobotDriver_OnListenCompleted(RobotDriver.ListenCompletedEventArgs args)
        {
            var hearWord = args.HearTheWord;
            if (!string.IsNullOrEmpty(args.HearTheWord))
                lastHasWord = DateTime.Now;
            if ((hearWord.Contains("问") && hearWord.Contains("答"))
                || (hearWord.Contains("问") && hearWord.Contains("题"))
                || (hearWord.Contains("知") && hearWord.Contains("识")))
            {
                Invoke(new MethodInvoker(() =>
                {
                    GoQaFrm();
                }));
            }
            else if ((hearWord.Contains("考") && hearWord.Contains("试"))
                     || (hearWord.Contains("闯") && hearWord.Contains("关"))
                     || (hearWord.Contains("答") && hearWord.Contains("题")))
            {
                Invoke(new MethodInvoker(() =>
                {
                    GoExamFrm();
                }));
            }
            else
            {
                Task.Factory.StartNew(() =>
                {
                    Thread.Sleep(1000);
                    RobotDriver.Instance.StartListen(nameof(SelectServiceFrm));
                });
            }
        }

        private void RobotDriver_OnListening(RobotDriver.ListeningEventArgs args)
        {
            
        }

        private void RobotDriver_OnSpeakCompleted(RobotDriver.SpeakCompletedEventArgs args)
        {
            Task.Factory.StartNew(() =>
            {
                Thread.Sleep(1000);
                RobotDriver.Instance.StartListen(nameof(SelectServiceFrm));
            });
        }

        private void RobotDriver_OnListenStart(RobotDriver.ListenStartEventArgs args)
        {
            Invoke(new MethodInvoker(() =>
            {
                this.picRobotState.Image = global::DGRobot.Properties.Resources.record;
                this.picRobotState.Tag = "listen";

            }));
        }

        private void RobotDriver_OnSpeakStart(RobotDriver.SpeakStartEventArgs args)
        {
            Invoke(new MethodInvoker(() =>
            {
                this.picRobotState.Image = global::DGRobot.Properties.Resources.speak;
                this.picRobotState.Tag = "speak";
            }));
        }

        private void GoQaFrm()
        {
            StopRobot();
            var form = new QAFrm();
            form.ShowDialog();
            StartRobot();
        }
        private void GoExamFrm()
        {
            StopRobot();
            var form = new QAFrm();
            form.ShowDialog();
            StartRobot();
        }
        private void btnQA_Click(object sender, EventArgs e)
        {
            GoQaFrm();
        }

        private void SetCtrlCenter(Control container,params Control[] controls)
        {
            if(controls == null || controls.Length<1)
            {
                return;
            }
            var contentWidth = controls.Sum(x => x.Width);
            var containerWidth = container.Width;
            var residue = containerWidth - contentWidth;
            var count = 2 + ((controls.Length-1)*2);
            var unitSpace = residue/(double) count;
            foreach (var control in controls)
            {
                control.Margin = new Padding((int)unitSpace);
            }
        }

        private void SelectServiceFrm_Load(object sender, EventArgs e)
        {
            SetCtrlCenter(this.flowLayoutPanel1, this.btnQA, this.btnExam);
            WelcomeFrm frm = new WelcomeFrm();
            frm.ShowDialog();
            StartRobot();
        }

        private void SelectServiceFrm_Resize(object sender, EventArgs e)
        {
            SetCtrlCenter(this.flowLayoutPanel1, this.btnQA, this.btnExam);
        }
        
        private void pictureBox1_Click(object sender, EventArgs e)
        {
            this.Close();
            //_mainFrm.GoPrevForm();
            //GoPrevFrm();
        }

        private void SelectServiceFrm_Enter(object sender, EventArgs e)
        {

        }

        private void SelectServiceFrm_Leave(object sender, EventArgs e)
        {

        }

        private void SelectServiceFrm_FormClosed(object sender, FormClosedEventArgs e)
        {
            RobotDriver.Instance.Dispose();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if ((DateTime.Now - lastHasWord).TotalMinutes > 2)
            {
                this.timer1.Stop();
                WelcomeFrm frm = new WelcomeFrm();
                frm.ShowDialog();
                StartRobot();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ConfigFrm configFrm = new ConfigFrm();
            configFrm.ShowDialog();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            ExitApp();
        }

        private void picRobotState_Click(object sender, EventArgs e)
        {
            var tag = this.picRobotState.Tag.ToString();
            if (tag == "speak")
            {
                RobotDriver.Instance.StopSpeak();
                RobotDriver.Instance.StartListen(nameof(SelectServiceFrm));
            }
            else
            {
                RobotDriver.Instance.StopListen();
                RobotDriver.Instance.StartListen(nameof(SelectServiceFrm));
            }
        }
    }
}
